package com.xujc.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/**
 * Created by xujc on 6/20/15.
 */
@WebServlet(name = "SelectServlet",urlPatterns = "/doselect")
public class selectServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
            PreparedStatement preparedStatement = con.prepareStatement("SELECT * FROM student WHERE sid=?");
            preparedStatement.setString(1,request.getParameter("sid"));
            ResultSet resultSet = preparedStatement.executeQuery();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect("/insert.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("Hello, 这是一个Servlet");
    }
}
